using System;

namespace SMS.SMPPClient.Util
{
    /// <summary>
    /// Utility class to do big-endian to little endian conversions and the like.
    /// This is for unsigned numbers only; use the IPAddress class for signed values.
    /// </summary>
    public sealed class UnsignedNumConverter
    {
        /// <summary>
        /// Do not instantiate.
        /// </summary>
        private UnsignedNumConverter()
        {
        }

        /// <summary>
        /// Converts from big-endian to little endian and vice versa.
        /// </summary>
        /// <param name="val">The value to swap.</param>
        /// <returns>The byte-swapped value.</returns>
        public static UInt32 SwapByteOrdering(UInt32 val)
        {
            return ((val << 24) & 0xFF000000) + ((val << 8) & 0x00FF0000) +
                     ((val >> 8) & 0x0000FF00) + ((val >> 24) & 0x000000FF);
        }

        /// <summary>
        /// Converts from big-endian to little endian and vice versa.  Don't use
        /// for negative integers; it has not been tested for that.
        /// </summary>
        /// <param name="val">The value to swap.</param>
        /// <returns>The byte-swapped value.</returns>
        public static UInt16 SwapByteOrdering(UInt16 val)
        {
            return (UInt16)(((val << 8) & 0xFF00) + ((val >> 8) & 0x00FF));
        }
    }
}
